<?php


namespace app\apibook\model;


use think\Model;

class BaseModel extends Model
{

    /**
     * @param $where
     * @return mixed
     * Created by chenyawei on 2024/8/7
     * Description:根据条件查询数量
     */
    public function checkExist($where){
        return $this->where($where)->count();
    }
    
    /**
     * @param $where 
     * @return array
     * Description:根据条件查询数量
     */
    public static function searchOne($where = array(),$fields='*',$orderBy='')
    {
        $query = self::where($where)->field($fields);
        if($orderBy){
            $query = $query->order($orderBy);
        }
        $query = $query->find();
        // $rs = self::where($where)->field($fields)->find();
        return $query ? $query->toArray():[];
    }
    /**
     * @param $where
     * @return array
     * @author wangsh
     * Description:根据条件查询数量
     */
    public static function searchAll($where = array(),$fields='*',$groupBy='id',$orderBy='')
    {
        $query = self::where($where)->field($fields);
        if($groupBy){
            $query = $query->group($groupBy);
        }
        if($orderBy){
            $query = $query->order($orderBy);
        }

        return $query->select()->toArray();
        //return self::where($where)->field($fields)->group($groupBy)->select()->toArray();
    }
    /**
     * @param $where array
     * @return object
     * @author wangsh
     * Description:返回的是当前模型的对象实例
     */
    public static function addOne($data = array()){
        return self::create($data);
    }
    /**
     * @param $where array
     * @param $data array
     * @author wangsh
     * @return int
     * Description:返回影响数据的条数，没修改任何数据返回 0
     */
    public static function edit($where = array(),$data = array()){
        return self::where($where)->update($data);
    }
    
    public static function setIncrease($where=array(),$singleField='',$incVal=0){
        return self::where($where)->setInc($singleField,$incVal);
    }
    
    //return  [1,2,3]
    public static function getSingleColumn($where,$singleField='id'){
        return self::where($where)->column($singleField);
    }
    
    public static function getSingleValue($where,$singleField='id'){
        return self::where($where)->value($singleField);
    }
    
    
    
    public function findByid($id){
        $item = $this->find($id);
        return $item ? $item->toArray():[];
    }
    
    public function findOneByCondition($where){
        $item = $this->where($where)->find();
        return $item ? $item->toArray():[];
    }

    public function findByCondition($where, $columns =['*']){
        $item = $this->where($where)->field($columns)->select();
        return $item ? $item->toArray():[];
    }

    public function getColumnByCondition($where, $column){
        return $this->where($where)->column($column);
    }

    public function getValueByCondition($where, $value){
        return $this->where($where)->value($value);
    }
    
}